package com.yoyo.ad.tencent;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.mobad.feeds.NativeResponse;
import com.qq.e.ads.interstitial.InterstitialAD;
import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.ads.nativ.NativeUnifiedADData;
import com.qq.e.ads.nativ.widget.NativeAdContainer;
import com.yoyo.ad.main.AdModelType;
import com.yoyo.ad.main.YoYoAd;
import com.qq.e.ads.nativ.NativeADDataRef;
import com.qq.e.comm.constants.AdPatternType;

import java.util.ArrayList;
import java.util.List;

import static com.yoyo.ad.confusion.AdManage.TENCENT;


class TencentYoYoAd implements YoYoAd {

    private boolean isNative;

    TencentYoYoAd(NativeADDataRef nativeADDataRef) {
        this.mNativeADDataRef = nativeADDataRef;
    }

    TencentYoYoAd(NativeUnifiedADData nativeUnifiedADData, Context context) {
        this.mNativeUnifiedADData = nativeUnifiedADData;
        this.mContext = context;
    }

    TencentYoYoAd(NativeExpressADView nativeExpressADView) {
        mNativeExpressADView = nativeExpressADView;
        isNative = true;
    }

    private NativeADDataRef mNativeADDataRef;
    private NativeUnifiedADData mNativeUnifiedADData;
    private NativeExpressADView mNativeExpressADView;
    private UnifiedInterstitialAD mInterstitialAD;
    private Context mContext;

    TencentYoYoAd(UnifiedInterstitialAD iad) {
        mInterstitialAD = iad;
    }

    @Override
    public int getSource() {
        return TENCENT;
    }

    @Override
    public String getTitle() {
        if (mNativeADDataRef != null) {
            return mNativeADDataRef.getTitle();
        }
        if (mNativeUnifiedADData != null) {
            return mNativeUnifiedADData.getTitle();
        }
        return null;
    }

    @Override
    public String getDescription() {
        if (mNativeADDataRef != null) {
            return mNativeADDataRef.getDesc();
        }
        if (mNativeUnifiedADData != null) {
            return mNativeUnifiedADData.getDesc();
        }
        return null;
    }

    @Override
    public Bitmap getLogo() {
        return null;
    }

    @Override
    public String getIconUrl() {
        if (mNativeADDataRef != null) {
            return mNativeADDataRef.getIconUrl();
        }
        if (mNativeUnifiedADData != null) {
            return mNativeUnifiedADData.getIconUrl();
        }
        return null;
    }

    @Override
    public String getImgUrl1() {
        if (mNativeADDataRef != null) {
            return (mNativeADDataRef.getAdPatternType() == AdPatternType.NATIVE_3IMAGE && mNativeADDataRef.getImgList() != null) ?
                    mNativeADDataRef.getImgList().get(0) : mNativeADDataRef.getImgUrl();
        }
        if (mNativeUnifiedADData != null) {
            return (mNativeUnifiedADData.getAdPatternType() == AdPatternType.NATIVE_3IMAGE && mNativeUnifiedADData.getImgList() != null) ?
                    mNativeUnifiedADData.getImgList().get(0) : mNativeADDataRef.getImgUrl();
        }
        return null;
    }

    @Override
    public String getImgUrl2() {
        if (mNativeADDataRef != null) {
            return (mNativeADDataRef.getAdPatternType() == AdPatternType.NATIVE_3IMAGE && mNativeADDataRef.getImgList() != null) ?
                    mNativeADDataRef.getImgList().size() > 1 ?
                            mNativeADDataRef.getImgList().get(1) : null : null;
        }
        if (mNativeUnifiedADData != null) {
            return (mNativeUnifiedADData.getAdPatternType() == AdPatternType.NATIVE_3IMAGE && mNativeUnifiedADData.getImgList() != null) ?
                    mNativeUnifiedADData.getImgList().size() > 1 ?
                            mNativeUnifiedADData.getImgList().get(1) : null : null;
        }
        return null;
    }

    @Override
    public String getImgUrl3() {
        if (mNativeADDataRef != null) {
            return (mNativeADDataRef.getAdPatternType() == AdPatternType.NATIVE_3IMAGE && mNativeADDataRef.getImgList() != null) ?
                    mNativeADDataRef.getImgList().size() > 2 ?
                            mNativeADDataRef.getImgList().get(2) : null : null;
        }
        if (mNativeUnifiedADData != null) {
            return (mNativeUnifiedADData.getAdPatternType() == AdPatternType.NATIVE_3IMAGE && mNativeUnifiedADData.getImgList() != null) ?
                    mNativeUnifiedADData.getImgList().size() > 2 ?
                            mNativeUnifiedADData.getImgList().get(2) : null : null;
        }
        return null;
    }

    @Override
    public String getBrandName() {
        if (mNativeADDataRef != null) {
            return "腾讯";
        }
        if (mNativeUnifiedADData != null) {
            return "腾讯";
        }
        return null;
    }

    @Override
    public int getModel() {
        if (mNativeADDataRef != null) {
            if (mNativeADDataRef.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
                return AdModelType.MODEL_VIDEO;
            } else {
                return AdModelType.MODEL_IMAGE;
            }
        }
        if (mNativeUnifiedADData != null) {
            if (mNativeUnifiedADData.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
                return AdModelType.MODEL_VIDEO;
            } else {
                return AdModelType.MODEL_IMAGE;
            }
        }
        return AdModelType.MODEL_UNKNOW;
    }

    @Override
    public View getView() {
        return mNativeExpressADView;
    }

    @Override
    public boolean isNativeExpress() {
        return isNative;
    }

    @Override
    public void exposure(View view) {
        if (mNativeADDataRef != null && view != null) {
            mNativeADDataRef.onExposured(view);
        }
        if (mNativeExpressADView != null) {
            mNativeExpressADView.render();
        }
    }

    @Override
    public void onAdClicked(final View view, final ViewGroup viewGroup) {
        if (viewGroup != null) {
            if (mNativeUnifiedADData != null && view != null) {
                List<View> clickableViews = new ArrayList<>();
                clickableViews.add(view);
                mNativeUnifiedADData.bindAdToView(mContext, (NativeAdContainer) viewGroup, null, clickableViews);
            } else {
                viewGroup.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mNativeADDataRef != null) {
                            mNativeADDataRef.onClicked(viewGroup);
                        }
                    }
                });
            }
        } else {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mNativeADDataRef != null) {
                        mNativeADDataRef.onClicked(view);
                    }
                }
            });
        }
    }

    @Override
    public void show(Activity activity) {
        if (mInterstitialAD != null) {
            mInterstitialAD.show();
        }
    }

    @Override
    public void release() {
        if (mNativeExpressADView != null) {
            mNativeExpressADView.destroy();
            mNativeExpressADView = null;
        }
    }
}
